package com.pospi.pospihold.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.pospi.pospihold.database.DB;
import com.pospi.pospihold.dto.MenuDto;

import java.util.List;

/**
 * Created by Qiyan on 2016/6/15.
 */
public class MenuDao {
    private DB menuDB;
    private String tableName = "menuinfo";
    private MenuDto menuDto;

    public MenuDao(Context context) {
        menuDB = new DB(context);
    }

    public void addMenuInfo(List<MenuDto> menuDtos) {
        SQLiteDatabase db = menuDB.getWritableDatabase();
        clearFeedTable(db);//先删除表格里面的所有的数据
        Log.d("添加数据", "开始添加数据");
        for (int i = 0; i < menuDtos.size(); i++) {
            MenuDto menuDto = menuDtos.get(i);
            db.execSQL("insert into goods(Sid,name,colorCode,orderBy)values(?,?,?,?)",
                    new Object[]{menuDto.getSid(), menuDto.getName(), menuDto.getColorCode(), menuDto.getOrderBy()});
            Log.i("添加一条数据", menuDto.getSid() + menuDto.getName() + menuDto.getColorCode() + menuDto.getOrderBy());
        }
        db.close();
    }

    /**
     * 在添加数据之前先删除表格里面所有的数据
     *
     * @param db
     */
    public void clearFeedTable(SQLiteDatabase db) {

        String sql = "DELETE FROM " + tableName + ";";

        db.execSQL(sql);
        Log.i("删除表格数据", "删除数据成功");

        revertSeq(db);
    }

    private void revertSeq(SQLiteDatabase db) {
        String sql = "update sqlite_sequence set seq=0 where name='" + tableName + "'";
        db.execSQL(sql);


    }
}
